[% setvar title Angle brackets should accept filenames and lists %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Angle brackets should accept filenames and lists</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Jon Ericson &lt;<a href='mailto:Jonathan.L.Ericson@jpl.nasa.gov'>Jonathan.L.Ericson@jpl.nasa.gov</a>&gt;
  Date: 8 Aug 2000
  Mailing List: <a href='mailto:perl6-language-io@perl.org'>perl6-language-io@perl.org</a>
  Number: 51
  Version: 2
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>The input operator should Do What I Mean.  If I pass it a filename, it
should try to open that file and readline it.  If I pass it a dirname,
it should open that directory and readdir it.  If I pass it a list of
scalars, it should try to open and read from them.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<a name='Background'></a><h2>Background</h2>
<p>Currently there are two modes of operation for &lt;&gt;.  The glob mode should
go away (RFC 34).  The input operator mode also has two modes: readline
from a filehandle and readline from a list of file names (the empty
filehandle).  Both modes are useful.</p>
<p>Currently directories have seperate handles and functions.</p>
<a name='Easy things should be easy'></a><h2>Easy things should be easy</h2>
<p>Input from a file is already easy in Perl, but it
could be easier.  Input from a directory is a pain in the neck.</p>
<a name='Flexibility'></a><h2>Flexibility</h2>
<p>As noted above, the behavior this RFC proposes is already partially
availible with @ARGV and &lt;&gt;.  But you can't mix filenames with
filehandles.  Nor is it obvious what you are doing.  And it takes two
steps.  Don't even think about using directory names.</p>
<a name='High Level Language'></a><h2>High Level Language</h2>
<p>The average Perl programmer should never have to open a file or
directory explicitly.  Error checking on opens is a pain and
repetative.  @ARGV and &lt;&gt; is confusing to almost everybody.  Wouldn't it
be nice to write:</p>
<pre>  while (&lt;grep(-f, &lt;~&gt;)&gt;){ # list of plain files in my home dir
     # do something with each line
  };
  print &lt;map { /\.(gz|Z)$/ ? &quot;gzip -dc &lt; $_ |&quot; : $_ } &lt;/src&gt;&gt;;</pre>
<p>as opposed to:</p>
<pre>  opendir my $dir, '~' or die &quot;can't open ~: $!&quot;;
  my @file = grep(-f, readdir $dir);
  @ARGV = @file;
  while(&lt;&gt;){
     # do something with each line
  };
  opendir my $other_dir, '/src' or die &quot;can't open /src: $!&quot;;
  @ARGV = map { /\.(gz|Z)$/ ? &quot;gzip -dc &lt; $_ |&quot; : $_ } readdir
$other_dir;
  print &lt;&gt;;</pre>
<p>The low level functions (open, readdir, etc.) should be there if you
need (or want) to use them.  Those of us who just want to open a file
and manipulate it line-by-line, can use the input operator.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<a name='&lt;LIST&gt;'></a><h2>&lt;LIST&gt;</h2>
<p>Angle brackets are the standard Perl input operators.</p>
<p>Each item in LIST is opened in a DWIM fashion.  Dir- and filehandles are
left alone.  Scalars that contain filenames (if (-f)) are opened as
filehandles.  Scalars that contain dirnames (if (-d)) are opened as
dirhandles.  Other scalars ('-', 'some_cmd |', etc.) are opened as
filehandles.  If any open fails, a warning is issued (or should it
die?).  The opened handle should be tightly bound to the scalar (or even
replace the scalar if stringified filehandles return the string passed
to open).</p>
<p>In list context, each item in LIST is opened and read in DWIM fashion.
Files are read a line at a time.  Directories are read a file at a time.</p>
<p>In scalar context, the next line (or file) is read from the first item
in LIST.  When the first item returns undef, the next item is read.
When there are no more items left, undef is returned.</p>
<p>Inside a while loop (or the conditional section of a for(;;) loop), $_
is assigned to the next line (or file) and tested for defined-ness.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<pre>  RFC 34 - Angle brackets should not be used for file globbing.
  perlop/&quot;I/O Operators&quot;</pre>
<a name='CHANGES'></a><h1>CHANGES</h1>
<ul>
<li><a name='v2'></a>v2</li>
<pre>  Added dirhandles.
  Reworded the ABSTRACT.
  Axed the perlop quote.
  Added to DESCRIPTION.
  Changed IMPLEMENTATION to a high level description.</pre>
</ul>
</div>
